import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
import router from '@/router';
export const userLoginStorage= defineStore('userLogin', () => {
  const user=ref(null);//存储登录用户对象
  const token=ref(null);//存储token
  //计算属性，判断是否已经登录
  const isAuthenticated=computed(()=>!!token.value)
  //登录逻辑
  function login(userData){
    user.value={
      realName:userData.realName,
      deptId:userData.deptId
    }
    token.value=userData.token;
    if(token.value!=null){
   localStorage.setItem('token',token.value);
    }
    if(user.value!=null){
    sessionStorage.setItem('deptId',user.value.deptId);
    sessionStorage.setItem('realName',user.value.realName);
    }
  }
  //登出函数
  function logout(){
    user.value=null;
    token.value=null;
    localStorage.removeItem('token');
    sessionStorage.removeItem('realName');
    sessionStorage.removeItem('deptId');
  }
  //设置token
  function checkToken(){
    const storedToken=localStorage.getItem('token');
    if(storedToken){
      token.value=storedToken;
    }else{
      token.value=null
    }
  }
  function checkTokenExpire(){

  }
  //返回所有工具类
   return {
    user,
    token,
    isAuthenticated,
    login,
    logout,
    checkToken,
   };
})
